package com.kcbsdk;

import android.util.Log;

import com.kcbsdk.MyDomain.AAnaDomain;
import com.kcbsdk.MyDomain.ADomainUser;
import com.kcbsdk.MyDomain.AListPage;
import com.kcbsdk.MyDomain.ATopDomain;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/5.
 */

public class CSvrDomain implements wpapper {
    public int g4EventMethod;

    public int pollingEvent(){
        return g4EventMethod | cjmkcs.systemPollingEvent(tDOMAIN, 0);
    }
    public void setMethodType(){
        cjmkcs.systemSetMethodType(tDOMAIN | 0x00000001);
    }
    public int getMethodType(){
        return cjmkcs.systemSetMethodType(tDOMAIN);
    }
    public String getMethodMessage(int type){
        return cjmkcs.systemGetMethodMessage(type);
    }


    public void setAnaDomainDbs(int type, String name, String password){
        if (null != name && null != password) {
            MSTRING("setAnaDomainDbs "+type + " "+name + " "+password);
            cjmkcs.domainSetAnaDomainDbs(type, name, password, DOMAIN_ANALYZE);
        }
    }
    public void fetchDomainUserList(int readType){
        cjmkcs.systemGetDatabaseList((readType << 14) | TOOLS_DBS_LIST_DDNS, 0, 0, 0, "",  DOMAIN_FETCH_DBS_LIST);
    }

    public void modifyDomainUser(String name, String password, int stepAnalyze, String urlRedirect, String urlRecord){
        if (null != name && null != password && null != urlRedirect && null != urlRecord) {
            cjmkcs.domainModifyDomainUser(name, password, stepAnalyze, urlRedirect, urlRecord, DOMAIN_MODIFY_USER);
        }
    }
    public void checkAvailableName(String name) {
        if (null != name) {
            cjmkcs.domainCheckAvailableName(name, DOMAIN_IS_AVAILABLE);
        }
    }
    public void addNewDomainUser(String name, String password, int stepAnalyze, String urlRedirect, String urlRecord, String authCode) {
        if (null != name && null != password && null != urlRedirect && null != urlRecord && null != authCode) {
            String mixText = urlRedirect + "," + urlRecord;
            cjmkcs.domainAddNewDomainUser(name, password, stepAnalyze, mixText, authCode, DOMAIN_ADD_NEW_USER);
        }
    }
    public void fetchPreUserNeedPrice(String name, int stepAnalyze, int month) {
        if (null != name) {
            cjmkcs.domainFetchPreUserNeedPrice(name, stepAnalyze, month, DOMAIN_READ_PRICE);
        }
    }
    public void renewalDomainUser(String name, int stepAnalyze, int renewTime) {
        if (null != name) {
            cjmkcs.domainRenewalDomainUser(name, stepAnalyze, renewTime, DOMAIN_USER_RENEWAL);
        }
    }
    public boolean isDnsSupportRedirect(String name) {
        if (null != name) {
            return (cjmkcs.domainIsDnsSupportRedirect(name) > 0) ? true : false;
        }
        return false;
    }


    public ADomainUser[] methodDomainUserDbs(int methodType, int[] retMax){
        int number = (methodType>>16) & 0xff;
        int indexQty = cjmkcs.systemDatabaseListIndex(0, number, 0);
        ADomainUser aDomainUser[] = null;
//MSTRING("methodDomainUserDbs A "+indexQty);
        if (indexQty > 0){
            retMax[0] = cjmkcs.systemDatabaseListIndex(1, number, 0);
            aDomainUser = new ADomainUser[indexQty];
            for (int counter = 0; counter < indexQty; counter++) {
                cjmkcs.systemDatabaseListIndex(2, number, counter);
                aDomainUser[counter] = new ADomainUser();
                aDomainUser[counter].aTopDomain = new ATopDomain();
                aDomainUser[counter].aTopDomain.Name = cjmkcs.systemGetWpapperString(wpDOMAIN_DomUserTopName | number);
                aDomainUser[counter].aTopDomain.IsSupportRedirect = (cjmkcs.systemGetWpapperValue(wpDOMAIN_DomUserTopType | number) > 0) ? true : false;
                aDomainUser[counter].Name = cjmkcs.systemGetWpapperString(wpDOMAIN_DomainName | number);
                aDomainUser[counter].RegisterDate = cjmkcs.systemGetWpapperString(wpDOMAIN_RegisterDate | number);
                aDomainUser[counter].ActiveDate = cjmkcs.systemGetWpapperString(wpDOMAIN_ActiveDate | number);
                aDomainUser[counter].ValidityPeriod = cjmkcs.systemGetWpapperString(wpDOMAIN_ValidityPeriod | number);
                aDomainUser[counter].ParsePassword = cjmkcs.systemGetWpapperString(wpDOMAIN_ParsePassword | number);
                aDomainUser[counter].LastIPAddr = cjmkcs.systemGetWpapperString(wpDOMAIN_LastIPAddr | number);
                aDomainUser[counter].SiteReference = cjmkcs.systemGetWpapperString(wpDOMAIN_SiteReference | number);
                aDomainUser[counter].RedirectURL = cjmkcs.systemGetWpapperString(wpDOMAIN_RedirectURL | number);
                aDomainUser[counter].AuditState = cjmkcs.systemGetWpapperString(wpDOMAIN_AuditState | number);
                aDomainUser[counter].ParseInterval = cjmkcs.systemGetWpapperValue(wpDOMAIN_ParseInterval | number);
                aDomainUser[counter].IsRedirect = cjmkcs.systemGetWpapperValue(wpDOMAIN_IsRedirect | number) > 0 ? true : false;
            }
        }
        cjmkcs.systemDatabaseListIndex(3, number, 0);
        return aDomainUser;        
    }

    public ATopDomain[] eventTopDomainDbs() {
        cjmkcs.systemClearEvent(tDOMAIN, DOMAIN_EVENT_TOP_DOMAIN, 0);
        boolean isSupportRedirect;
        int retSize = cjmkcs.systemGetWpapperValue(wpDOMAIN_TopDomainQty);
        ATopDomain aTopDomain[] = new ATopDomain[retSize];
        for (int counter = 0; counter < retSize; counter++){
            aTopDomain[counter] = new ATopDomain();
            aTopDomain[counter].Name = cjmkcs.systemGetWpapperString(wpDOMAIN_TopDomainName | counter);
            isSupportRedirect = cjmkcs.systemGetWpapperValue(wpDOMAIN_TopDomainType | counter) > 0 ? true : false;
            aTopDomain[counter].IsSupportRedirect = isSupportRedirect;
        }
        g4EventMethod |= DOMAIN_EVENT_TOP_DOMAIN;
        return aTopDomain;
    }
    public AAnaDomain[] eventAnaDomainDbs(){
        cjmkcs.systemClearEvent(tDOMAIN, DOMAIN_EVENT_DBS_ANALYZE, 0);
        int retSize = cjmkcs.systemGetWpapperValue(wpDOMAIN_AnaDomainQty);
//MSTRING("isEventAnaDomainDbs " + retSize);
        AAnaDomain aAnaDomain[] = new AAnaDomain[retSize];
        int g4Local_1;
        for (int counter = 0; counter < retSize; counter++) {
            aAnaDomain[counter] = new AAnaDomain();
            aAnaDomain[counter].DomainName = cjmkcs.systemGetWpapperString(wpDOMAIN_AnaDomainName | counter);
            aAnaDomain[counter].parsePassword = cjmkcs.systemGetWpapperString(wpDOMAIN_AnaDomainPsw | counter);
            aAnaDomain[counter].OwnerKcid = cjmkcs.systemGetWpapperValue(wpDOMAIN_AnaDomainKcid | counter);
            aAnaDomain[counter].ParseInterval = cjmkcs.systemGetWpapperValue(wpDOMAIN_AnaDomainTime | counter);
            g4Local_1 = cjmkcs.systemGetWpapperValue(wpDOMAIN_AnaDomainType | counter);
            aAnaDomain[counter].IsTypeDevice = ((g4Local_1 & 0x01) > 0) ? true : false;
            aAnaDomain[counter].IsTypeDirect = ((g4Local_1 & 0x02) > 0) ? true : false;
//MSTRING(String.format("g4Local_1 %d %d %s %s", counter, g4Local_1, aAnaDomain[counter].IsTypeDevice, aAnaDomain[counter].IsTypeDirect));
        }
        g4EventMethod |= DOMAIN_EVENT_DBS_ANALYZE;
        return aAnaDomain;
    }
    public int eventIPAddress() {
        cjmkcs.systemClearEvent(tDOMAIN, DOMAIN_EVENT_IP_ARRDRESS, 0);
        return cjmkcs.systemGetWpapperValue(wpDOMAIN_IPAddress);
    }
//    void CPP eventRtcText(CString& retText, int & retIndex) {
//        cjmkcs.systemClearEvent(tDOMAIN, DOMAIN_EVENT_RTC_TEXT, 0);
//        retIndex = cjmkcs.systemGetWpapperValue(wpDOMAIN_RemoteIndex);
//        retText = cjmkcs.systemGetWpapperString(wpDOMAIN_RemoteRtcText);
//        g4EventMethod |= DOMAIN_EVENT_RTC_TEXT;
//    }








    public static void MSTRING(String text) {
        Log.d("MSYS", text);
    }

}
